
import Taro, { Component, useEffect, useState } from '@tarojs/taro';
import { View, Image } from '@tarojs/components';
import './index.scss';
import { fullImgUrl, timeFormat } from '../../utils/utils';
import { setting as getSetting } from '../../services/program';
import { updateOrderTime } from '../../services/order';

function UpdateModal({ orderInfo, cancel }) {

  const [selectedTime, setSelectedTime] = useState('');
  const [startDeliveryTime, setStartDeliveryTime] = useState('');

  useEffect(() => {
    setSelectedTime(orderInfo.delivery_time);
    let temp = orderInfo.delivery_time;
    temp = temp.replace('年', '-');
    temp = temp.replace('月', '-');
    temp = temp.replace('日', '');
    let date = new Date();
    date.setDate(date.getDate() + 1);
    // 下午三点半以后，后天再发货
    if (date.getHours() > 15 || (date.getHours() === 15 && date.getMinutes() >= 30)) {
      date.setDate(date.getDate() + 1);
    }
    setStartDeliveryTime(timeFormat(date.getTime() / 1000, 'yyyy-MM-dd'));
  }, [orderInfo])

  function dateChange(e) {
    if (e.detail.value == '2021-02-11' || e.detail.value == '2021-02-12' || e.detail.value == '2021-02-13') {
      Taro.showToast({
        title: '春节期间不发货,请重新选择发货时间!',
        icon: 'none'
      });

    } else {
      let temp = e.detail.value.split('-');
      setSelectedTime(`${temp[0]}年${temp[1]}月${temp[2]}日`);
    }

  }

  function update() {
    updateOrderTime({
      order_id: orderInfo.order_info_id,
      delivery_time: selectedTime,
    }).then(res => {
      cancel();
      if (notifyParentRefresh) notifyParentRefresh();
    })
  }

  return (
    <View className="update-order__wrapper">
      <View className="update-order__mask" onClick={cancel} />
      <View className="update-order__item-wrapper">
        {/* <View className="update-order__item">
          <Text className="update-order__item__text">地址</Text>
          <Input className="update-order__item__input" value={editAddress} onInput={e => {setEditAddress(e.detail.value)}} />
        </View> */}
        <View className="update-order__item">
          <Text className="update-order__item__text">发货时间</Text>
          <Picker className="date-picker" value={selectedTime} mode='date' start={startDeliveryTime} onChange={dateChange}>
            <View className="number">{selectedTime}></View>
          </Picker>
        </View>
        <View className="update-order__opt-wrapper">
          <View className="update-order__btn-check" onClick={update}>确认</View>
          <View className="update-order__btn-cancel" onClick={cancel}>取消</View>
        </View>
      </View>
    </View>
  )
}
